package com.concerneyes.rentbarsecond.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

/**
 * Depiction:租吧数据库操作
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date：2016年11月15日 下午3:32:11
 * <p>
 *
 * @version 1.0
 * @since 1.0
 */
public class RentBarDao {
    private Dao<GlassesDetailModelForDao, Integer> dao;

    public RentBarDao(Context context) {
        if (dao == null) {
            try {
                dao = DBRentBarHelper.getHelper(context).getDao(GlassesDetailModelForDao.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isEmpty() {
        try {
            return dao.countOf() == 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }

    public void add(GlassesDetailModelForDao model) {
        if (model == null) {
            Log.e(getClass().getSimpleName(), "add()  the model is null");
            return;
        }
        try {
//			if (exist(model.getCategoryId())) {
//				GlassesDetailModelForDao temp = get(model.getCategoryId());
//				dao.delete(temp);
//			}
            dao.create(model);
        } catch (Exception e1) {
            Log.e(getClass().getSimpleName(), "add()  ParseException date -->" + e1.toString());
        }
    }

    public void delete(int id) {
        try {
            GlassesDetailModelForDao model = get(id);
            if (model != null) {
                dao.deleteById(model.get_id());
            } else {
                Log.e(getClass().getSimpleName(), "delete()-->the model is null");
            }
        } catch (SQLException e) {
            Log.e(getClass().getSimpleName(), "delete()-->" + e.toString());
        }

    }

    public void clear() {
        try {
            dao.queryRaw("delete from t_warning");
        } catch (SQLException e) {
            Log.e(getClass().getSimpleName(), "clear()-->" + e.toString());
        }
    }

    public void updata(GlassesDetailModelForDao model) {
        try {
            dao.update(model);
        } catch (SQLException e) {
            Log.e(getClass().getSimpleName(), "clear()-->" + e.toString());
        }
    }

    public GlassesDetailModelForDao get(int id) {
        try {
            return dao.queryBuilder().where().eq("_id", id).queryForFirst();
        } catch (SQLException e) {
            Log.e(getClass().getSimpleName(), "get()-->" + e.toString());
        }
        return null;
    }

    public List<GlassesDetailModelForDao> getAllValid() {
        try {
            Log.e(getClass().getSimpleName(), "count-->" + dao.countOf());
//			return dao.queryBuilder().where().gt("endTime", System.currentTimeMillis()).query();
            return dao.queryBuilder().orderBy("_id", false).query();
//			return dao.queryForAll();
        } catch (SQLException e) {
            Log.e(getClass().getSimpleName(), "getAllValid()-->" + e.toString());
        }
        return null;
    }

    public boolean exist(int id) {
        return get(id) != null;
    }

}
